package com.colin.ui.component;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class ChooseFrame extends JPanel {
    private List<String> selectedItems = new ArrayList<>();
    private JPanel selectedPanel = new JPanel(new GridBagLayout());
    private JComboBox<String> tableComboBox;
    private final JTextField authorPanel = new JTextField();
    private JComboBox<String> moduleComboBox;

    public ChooseFrame(String[] tableItems, String[] moduleItems) {
        tableComboBox = new MyComboBox(tableItems);
        moduleComboBox = new MyComboBox(moduleItems);
        tableComboBox.setEditable(true);
        setLayout(new GridLayout(1, 2));

        tableComboBox.setEditable(true);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 1.0;

        JPanel comboBoxPanel = new JPanel(new GridLayout(5, 2, 3, 3));
        JLabel jLabel = new JLabel("table:");
        comboBoxPanel.add(jLabel);
        comboBoxPanel.add(tableComboBox);
        JLabel label = new JLabel("author:");
        comboBoxPanel.add(label);
        comboBoxPanel.add(authorPanel);
        JLabel moduleLabel = new JLabel("module:");
        comboBoxPanel.add(moduleLabel);
        comboBoxPanel.add(moduleComboBox);

        JPanel contentPanel = new JPanel(new BorderLayout());
        contentPanel.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 10));
        contentPanel.add(comboBoxPanel, BorderLayout.NORTH);

        add(contentPanel);
        add(new JScrollPane(selectedPanel));

        tableComboBox.addActionListener(e -> {
            String selectedItem = (String) tableComboBox.getSelectedItem();
            if (selectedItem != null && !selectedItems.contains(selectedItem)) {
                selectedItems.add(selectedItem);
                addSelectedItem(selectedItem, gbc);
                tableComboBox.setSelectedItem(null);
            }
        });
    }

    private void addSelectedItem(String item, GridBagConstraints gbc) {
        JPanel itemPanel = new JPanel(new BorderLayout());
        itemPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        itemPanel.add(new JLabel(item), BorderLayout.WEST);

        JButton deleteButton = new JButton("×");
        deleteButton.addActionListener(e -> {
            selectedItems.remove(item);
            selectedPanel.remove(itemPanel);
            selectedPanel.revalidate();
            selectedPanel.repaint();
        });

        itemPanel.add(deleteButton, BorderLayout.EAST);
        selectedPanel.add(itemPanel, gbc);
        selectedPanel.revalidate();
        selectedPanel.repaint();
    }

    public List<String> getSelectedTabels() {
        return selectedItems;
    }

    public String getAuthor() {
        return authorPanel.getText();
    }

    public String getModule() {
        return (String) moduleComboBox.getSelectedItem();
    }

//    public static void main(String[] args) {
//        JFrame jFrame = new JFrame();
//        jFrame.setTitle("Custom Multi-Select Combo Box Demo");
//        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        jFrame.setLayout(new BorderLayout());
//        jFrame.setBounds(100, 100, 800, 400);
//        ChooseFrame chooseFrame = new ChooseFrame(new String[]{"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"});
//        jFrame.add(chooseFrame);
//        SwingUtilities.invokeLater(() -> {
//            try {
//                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            jFrame.setVisible(true);
//        });
//    }
}
